package com.heyqing.topic.ContainsNearbyDuplicate_0219;

import java.util.*;

/**
 * ClassName:ContainsNearbyDuplicate
 * Package:com.heyqing.topic.ContainsNearbyDuplicate_0219
 * Description:
 *
 * @Date:2024/9/22
 * @Author:Heyqing
 */
public class ContainsNearbyDuplicate {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer minI = map.get(nums[i]);
            if (Objects.nonNull(minI)) {
                if (Math.abs(i - minI) <= k) return true;
            }
            map.put(nums[i], i);
        }
        return false;
    }
}
